--# Main
-- 接鸡蛋
-- supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup

--[[
##########################################
# author:lhc1071426378 #
# tieba:@1071426378 #
#地址:http://tieba.baidu.com/p/4561737269#
##########################################
]]--


function setup()
displayMode(FULLSCREEN)
egg = {x = 0,y = HEIGHT}
board = {x = math.random(200,WIDTH),y = HEIGHT / 4}
start = 0
loc = 0
fall = 0
crash = 0
score = 0
boom = 0
-- 改变speed的值可以改变蛋的下落速度,从而改变难度
speed = 0.8
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color 
background(0, 211, 255, 255)

-- This sets the line thickness


-- Do your drawing here
::a::
if start == 0 then
fill(255, 255, 255, 255)
fontSize(85)
textMode(CENTER)
text("轻触屏幕以开始",WIDTH / 2,(HEIGHT - 125) / 2)
end 
if start == 1 then
fill(0, 0, 0, 255)
if board.x - 50 < 0 then
board.x = 50
end
if board.x + 50 > WIDTH then
board.x = WIDTH - 50
end
rect(board.x - 50,board.y,100,25)
if fall == 0 then
if crash >= 3 then
start = 2
goto a
end
make_new()
fall = 0.5
end
if fall == 0.5 then
tween(speed,egg,{y = HEIGHT / 4 + 55 / 2 + 25},tween.easing.linear,function()
if egg.x > board.x - (50 + 45 / 2) and egg.x < board.x + (50 + 45 / 2) then
fall = 0
if boom == 0 then
score = score + 10
else
crash = crash + 1
end
else tween(speed / 2,egg,{y = -55 / 2},tween.easing.linear,function()
if boom == 0 then
crash = crash + 1
end
fall = 0
end)
end
end)
fall = 1
end
strokeWidth(5)
if boom == 0 then
fill(255, 186, 0, 255)
else
fill(0, 0, 0, 255)
end
ellipse(egg.x,egg.y,45,55)
end
noStroke()
fill(227, 207, 17, 255)
rect(0,HEIGHT - 125,125,125)
rect((WIDTH - 750) / 5 + 125,HEIGHT - 125,125,125)
rect(((WIDTH - 750) / 5 + 125) * 2,HEIGHT - 125,125,125)
rect(((WIDTH - 750) / 5 + 125) * 3,HEIGHT - 125,125,125)
rect(((WIDTH - 750) / 5 + 125) * 4,HEIGHT - 125,125,125)
rect(((WIDTH - 750) / 5 + 125) * 5,HEIGHT - 125,125,125)
if start == 1 then
fontSize(20)
fill(255, 255, 255, 255)
textMode(CORNER)
text("score:" .. tostring(score),0,HEIGHT - 20)
textMode(CENTER)
fontSize(85)
end
if crash >= 1 then
fill(255, 0, 0, 255)
text("x",20,30)
end
if crash >= 2 then
fill(255, 0, 0, 255)
text("x",60,30)
end
if crash >= 3 then
fill(255, 0, 0, 255)
text("x",100,30)
end
if start == 2 then
textMode(CENTER)
fontSize(85)
fill(255, 255, 255, 255)
text("游戏结束",WIDTH / 2,(HEIGHT - 125) / 2 + 85 / 2)
text("分数：" .. tostring(score),WIDTH / 2,(HEIGHT - 125) / 2 - 85 / 2)
end
end

function make_new()
loc = math.random(1,6)
if loc == 1 then
egg.x = 125 / 2
end
if loc == 2 then
egg.x = (WIDTH - 750) / 5 + 125 + 125 / 2
end
if loc == 3 then
egg.x = ((WIDTH - 750) / 5 + 125) * 2 + 125 / 2
end
if loc == 4 then
egg.x = ((WIDTH - 750) / 5 + 125) * 3 + 125 / 2
end
if loc == 5 then
egg.x = ((WIDTH - 750) / 5 + 125) * 4 + 125 / 2
end
if loc == 6 then
egg.x = ((WIDTH - 750) / 5 + 125) * 5 + 125 / 2
end
egg.y = HEIGHT
loc = math.random(1,100)
if loc <= 20 then
boom = 1
else
boom = 0
end
end

function touched(touch)
if touch.state == BEGAN and start == 0 then
start = 1
end
if (touch.state == MOVING or touch.state == BEGAN) and start == 1 then
board.x = touch.x
end
if touch.state == BEGAN and start == 2 then
egg = {x = 0,y = HEIGHT}
loc = 0
fall = 0
crash = 0
score = 0
start = 0
end
end